<?php
if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

require_once(S_ROOT.'./site/libs/func_util.php');
include_once(S_ROOT.'./site/libs/func_model.php');

if ($_GET['op'] == 'newp') {
    
    //实名认证
    ckrealname('share');

    //新用户见习
    cknewuser();

    if(empty($_SGLOBAL['supe_uid'])) {
        echo json_encode(retrieve_login_form());
        exit;
    }

    if(!submitcheck('spotsubmit'))
    {
        $profid = empty($_GET['profid'])?0:$_GET['profid'];

        ob_start();
        include_once template('site/tpl/create_spot');
       	$bgcontent = ob_get_contents();
        ob_end_clean();

        $r['html']=$bgcontent;
        echo json_encode($r);
        exit;
    }
    else
    {
        $r['err'] = $r['step'] = 0;
        $profid = empty($_GET['profid'])?0:intval($_GET['profid']);
        $profile_name =  empty($_POST['profile_name'])?'':$_POST['profile_name'];
        $general = empty($_POST['general'])?'':$_POST['general'];
        
        if(strlen($profile_name) < 2) {
            $r['err'] = "档案名不能小于1个中文字(2个英文字符)";
            echo json_encode($r);
            exit;
        }

        if(strlen($profile_name) > 50) {
            $r['err'] = "档案名不能多于25个中文字(50个英文字符)";
            echo json_encode($r);
            exit;
        }

        if(strlen($general) && (strlen($general) > 200)) {
            $r['err'] = "说明请保持在100个汉字之内";
            echo json_encode($r);
            exit;
        } elseif (strlen($general) && strlen($general) < 2) {
            $r['err'] = "说明至少填一个汉字！";
            echo json_encode($r);
            exit; 
        }

        $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagname='$profile_name'");
        if($mtag = $_SGLOBAL['db']->fetch_array($query)) {
            if ($mtag['tagtype']) {
                //判断是否存在
                $tagid = $mtag['tagid'];
                
		if($profid == $tagid) {
		    $r['err'] = "不能添加自己与自己的发现！";
                    echo json_encode($r);
		    exit;
		}
                $arr = array();
                $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagid='$profid' AND tagtype='1'");
                if(!$mtag2 = $_SGLOBAL['db']->fetch_array($query)) {
                    // showmessage('designated_election_it_does_not_exist');
                    // TODO : add the alert of wrong profile 1.
                    //
                    $r['err'] = "不存在该档案";
                    echo json_encode($r);
					exit;
                }
                $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spot')." WHERE (tag1='$profid' AND tag2='$tagid') or (tag2='$profid' AND tag1='$tagid')");
                if(!$exists = $_SGLOBAL['db']->fetch_array($query)) {
                    // showmessage('designated_election_it_does_not_exist');
                    // TODO : add the alert of wrong profile 1.
                    //
                    $arr['spotnote'] = getstr($general, 150, 1, 1, 1, 1);

                    $arr['tag1'] = $profid;
                    $arr['tag2'] = $tagid;

                    $arr['uid'] = $_SGLOBAL['supe_uid'];
                    $arr['username'] = $_SGLOBAL['supe_username'];

                    $arr['dateline'] = $_SGLOBAL['timestamp'];
                    $arr['updatetime'] = $_SGLOBAL['timestamp'];

                    $sid = inserttable('spot', $arr, 1);

					//事件通知
					$matg1=getprof($arr['tag1']);
					$matg2=getprof($arr['tag2']);
					$fs = array();
					$fs['icon'] = 'spot';
					$fs['title_template'] = cplang('feed_spot_create');
					$fs['title_data'] = array('tagname' => "<a href=\"site.php?ac=image&type=spot&id=$sid\">$matg1[tagname] 和 $matg2[tagname]</a>");
					$fs['target_ids'] = "$sid";
					feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], null, null, null, null, null, $fs['target_ids']);

                    $_SGLOBAL['db']->query("UPDATE ".tname('mtag')." SET spotcnt=spotcnt+1 WHERE tagid=".$profid);
                    $_SGLOBAL['db']->query("UPDATE ".tname('mtag')." SET spotcnt=spotcnt+1 WHERE tagid=".$tagid);

                    $r['spotid'] = $sid; 
                    echo json_encode($r);
                    exit;

                } else {
                    $r['err'] = "该发现已经存在";
                    echo json_encode($r);
                    exit;
                }
            }
        } else {
            // FIXME: not exist, then let user create a profile now
            //
            $r['err'] = $r['step'] = 1;
            ob_start();
            $cateselect = '<option value=0>   </option>\n';
            $subcatselect = "case \"0\":\n";
            $subcatselect .= "var sfOptions = new Array(";
            $subcatselect .= "\t\"\", \"0\");";
            $subcatselect .= "\t break;";
            $sfid = 0;
            if (!isset($fid)) {
                foreach ($_SGLOBAL['profield'] as $p) {
                    if (!$p['parentid'] && $p['profieldtype']) {
                        $cateselect .= "<option value=\"".$p[fieldid]."\">".$p[title]."</option>\n"; 
                        $subcatselect.= "case \"".$p[fieldid]."\":\n";
                        $subcatselect.= "var sfOptions = new Array(";
                        $subcatselect.= "\" \", \"0\",\n";
                        foreach ($_SGLOBAL['profield'] as $k) {
                            if ($k['parentid'] == $p['fieldid']) {
                                 $subcatselect .= "\"".$k['title']."\", \"".$k['fieldid']."\",\n"; 
                            }
                        }
                        $subcatselect = substr($subcatselect,0,-2) ."\t);\n";
                        $subcatselect .= "\tbreak;\n";
                    }
                }
            }
            include_once template('site/tpl/create_profile');
            $bgcontent = ob_get_contents();
            ob_end_clean();

            $r['html']=$bgcontent;
            echo json_encode($r);
     
            exit;
        }
        
        $r['err'] = 'oops';
        echo json_encode($r);
        exit;

        if (!isset($tagid))
            return;
   }
} elseif($op == 'report') {
	
	$_GET['idtype'] = trim($_GET['idtype']);
	$_GET['id'] = intval($_GET['id']);
	$uidarr = $report = array();

    if (!$_SGLOBAL['supe_uid']) {
        echo json_encode(retrieve_login_form());
        exit;
    }
    
    $space['uid'] = $_SGLOBAL['supe_uid'];
    $space['username'] = $_SGLOBAL['supe_username'];

    if(!in_array($_GET['idtype'], array('picid', 'blog', 'album', 'mtag', 'thread', 'share', 'space', 'profile', 'spot', 'articleid', 'imageid')) || empty($_GET['id'])) {
    	//showmessage('report_error');
        $r['err'] = "无效的报告参数！";
        echo json_encode($r);
        exit;
    }
	//获取举报记录
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('report')." WHERE id='$_GET[id]' AND idtype='$_GET[idtype]'");
	if($report = $_SGLOBAL['db']->fetch_array($query)) {
		$uidarr = unserialize($report['uids']);
		if($uidarr[$space['uid']]) {
			//showmessage('repeat_report');
            $r['err'] = $_SGLOBAL['msglang']['repeat_report'];
            echo json_encode($r);
            exit;
		}
	}
	
	if(submitcheck('reportsubmit')) {
		$reason = getstr($_POST['reason'], 150, 1, 1);
		
		$reason = "<li><strong><a href=\"space.php?uid=$space[uid]\" target=\"_blank\">$_SGLOBAL[supe_username]</a>:</strong> ".$reason.' ('.sgmdate('m-d H:i').')</li>';
		
		if($report) {
			$uidarr[$space['uid']] = $space['username'];
			$uids = addslashes(serialize($uidarr));
			$reason = addslashes($report['reason']).$reason;
			$_SGLOBAL['db']->query("UPDATE ".tname('report')." SET num=num+1, reason='$reason', dateline='$_SGLOBAL[timestamp]', uids='$uids' WHERE rid='$report[rid]'");
		} else {
			$uidarr[$space['uid']] = $space['username'];
			
			$setarr = array(
				'id' => $_GET['id'],
				'idtype' => $_GET['idtype'],
				'num' => 1,
				'new' => 1,
				'reason' => $reason,
				'uids' => addslashes(serialize($uidarr)),
				'dateline' => $_SGLOBAL['timestamp']
			);
			inserttable('report', $setarr);
		}

		$r['html'] = $_SGLOBAL['msglang']['report_success'];
        echo json_encode($r);
        exit;
	} 
	
	//判断是否是被忽略的举报
	if(isset($report['num']) && $report['num'] < 1) {
		$r['err'] = $_SGLOBAL['msglang']['the_normal_information'];
        echo json_encode($r);
        exit;
	}
	
	$reason = explode("\r\n", trim(preg_replace("/(\s*(\r\n|\n\r|\n|\r)\s*)/", "\r\n", data_get('reason'))));
	if(is_array($reason) && count($reason) == 1 && empty($reason[0])) {
		$reason = array();
	}

    ob_start();
    include_once template('site/tpl/ajax_report');
    $bgcontent = ob_get_contents();
    ob_end_clean();

    $r['html'] = $bgcontent;
    echo json_encode($r);
    exit;    
} 
else
{
    ;
}

?>
